package com.java.collection.comparer;

import java.util.*;

import org.junit.Test;

public class ComparerEx implements Comparator<Manager>{
	@Test
	public void testEx(){
		Manager m1 = new Manager(21,"zzzz");
		Manager m2 = new Manager(31,"llll");
		Manager m3 = new Manager(41,"wwww");
		Manager m4 = new Manager(29,"dddd");
		Manager m5 = new Manager(11,"ssss");
		List<Manager> list = new ArrayList<Manager>();
		//Set<Manager> list = new TreeSet<Manager>();
		list.add(m1);
		list.add(m2);
		list.add(m3);
		list.add(m4);
		list.add(m5);
		System.out.println("未排序：" + list);
		System.out.println("---------------------------------------------");
		Collections.sort(list);//升序
		System.out.println("排序："+list);
		System.out.println("---------------------------------------------");
		Collections.sort(list, this);
		System.out.println("排序："+list);
		Comparator comparator = Collections.reverseOrder();
		Collections.sort(list, comparator);
		System.out.println("排序："+list);
	}

	@Test
	public void testEx2(){
		Manager m1 = new Manager(21,"zzzz");
		Manager m2 = new Manager(31,"llll");
		Manager m3 = new Manager(41,"wwww");
		Manager m4 = new Manager(29,"dddd");
		Manager m5 = new Manager(11,"ssss");
		Set<Manager> set = new TreeSet<Manager>(this);
		set.add(m1);
		set.add(m2);
		set.add(m3);
		set.add(m4);
		set.add(m5);
		System.out.println("排序："+set);
	}

	@Override
	public int compare(Manager obj1, Manager obj2) {
		//System.out.println(obj1.getName()+"-------->"+obj2.getName());
		// 后面的元素和前面的元素比，大于0, 交换，降序
		return obj2.getName().compareTo(obj1.getName());
	}
	
}
